<template>
  <j-modal
    title="确认出库明细"
    :visible="value"
    width="80%"
    @cancel="handleCancel"
    :cancelText="$t('button.closeTxt')"
    @ok="submitFun"
  >
    <fmsx-form ref="from" :options="formOption" :model="model" @submit="onSubmitFun"> </fmsx-form>
  </j-modal>
</template>
<script>
import wipApi from '@/api/fmsx/wip'
export default {
  name: 'OutboundItem',
  desc: '出库明细确认',
  data() {
    return {
      formOption: {
        details: false,
        isSubmitBtn: false,
        isEmptyBtn: false,
        group: [
          {
            icon: '',
            label: '',
            column: [
              {
                label: '',
                prop: 'formTable',
                type: 'formTable',
                span: 24,
                children: {
                  type: 'table',
                  isAdd: false,
                  isInsert: false,
                  isDelete: false,
                  isShowColumn: false,
                  checkBox: true,
                  column: [
                    {
                      label: '工单号',
                      prop: 'moNum',
                      align: 'left',
                      width: 100,
                      details: true
                    },
                    {
                      label: '客户代码',
                      prop: 'custCode',
                      align: 'left',
                      width: 100,
                      details: true
                    },
                    {
                      label: '图号',
                      prop: 'dwgNo',
                      align: 'left',
                      details: true,
                      width: 200
                    },
                    {
                      label: '版本',
                      prop: 'rev',
                      align: 'left',
                      width: 100,
                      details: true
                    },
                    {
                      label: '工序顺序',
                      prop: 'operationNum',
                      details: true,
                      display: false,
                      value: ''
                    },
                    {
                      label: '工序代码',
                      prop: 'processId',
                      details: true,
                      display: false,
                      value: ''
                    },
                    {
                      label: '工序名称',
                      type: 'input',
                      prop: 'processName',
                      details: true,
                      display: false
                    },
                    {
                      label: '数量',
                      prop: 'outQuantity',
                      details: true
                    },
                    {
                      label: '仓库',
                      prop: 'wmsId',
                      type: 'fmsxSelect',
                      rules: [{ required: true, message: '' }],
                      width: 150,
                      batchEdit: true,
                      options: {
                        url: '/prod-post/wms/wmsInfo/list?column=updateTime&order=desc&type=30',
                        isPagination: true,
                        prop: { label: 'whName', value: 'id' },
                        defaultDataProp: { label: 'wmsName', value: 'wmsId' },
                        params: () => {
                          return { type: 10 }
                        }
                      },
                      events: {
                        rowChange: (model, itemOption, rowOption, rowIndex, target) => {
                          if (model.shelvesId && rowOption && target != 'blur') {
                            Object.keys(rowOption).forEach((key) => {
                              if (key == 'shelvesId') {
                                model.shelvesId = ''
                                model.shelvesName = ''
                                rowOption[key].options.defaultData = []
                                rowOption[key].options.value = ''
                              }

                              if (key == 'storageId') {
                                model.storageId = ''
                                model.storageName = ''
                                rowOption[key].options.defaultData = []
                                rowOption[key].options.value = ''
                              }
                            })
                          }
                        }
                      }
                    },
                    {
                      label: '货架',
                      prop: 'shelvesId',
                      type: 'fmsxSelect',
                      batchEdit: true,
                      rules: [{ required: true, message: '' }],
                      width: 150,
                      options: {
                        prop: { label: 'shName', value: 'id' },
                        defaultDataProp: { label: 'shelvesName', value: 'shelvesId' },
                        url: '/prod-post/wms/wmsShelves/list?column=updateTime&order=desc&type=30',
                        isPagination: true,
                        params: (rulesModel) => {
                          if (rulesModel.wmsId) {
                            return { wmsId: rulesModel.wmsId }
                          } else {
                            return { wmsId: -1 }
                          }
                        }
                      },
                      events: {
                        rowChange: (model, itemOption, rowOption, rowIndex, target) => {
                          this.selectItem = model
                          if (model.storageId && target != 'blur') {
                            Object.keys(rowOption).forEach((key) => {
                              if (key == 'storageId') {
                                model.storageId = ''
                                model.storageName = ''
                                rowOption[key].options.defaultData = []
                                rowOption[key].options.value = ''
                              }
                            })
                          }
                        }
                      }
                    },
                    {
                      label: '储位',
                      prop: 'storageId',
                      type: 'fmsxSelect',
                      batchEdit: true,
                      rules: [{ required: true, message: '' }],
                      width: 150,
                      options: {
                        prop: { label: 'locName', value: 'id' },
                        defaultDataProp: { label: 'storageName', value: 'storageId' },
                        url: '/prod-post/wms/wmsStoLocation/list?column=updateTime&order=desc&type=30',
                        isPagination: true,
                        params: (rulesModel) => {
                          if (rulesModel.wmsId && rulesModel.shelvesId) {
                            return { wmsId: rulesModel.wmsId, wmsShelvesId: rulesModel.shelvesId }
                          } else {
                            return { wmsId: -1, wmsShelvesId: -1 }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        ],
        model: {}
      },
      model: {},
      selectItem: {}
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    applyEntryId: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  mounted() {
    this.getInboundApplyItem()
  },
  methods: {
    getInboundApplyItem() {
      wipApi.outbound
        .getItemList({
          applyEntryId: this.applyEntryId.join(','),
          pageSize: -1
        })
        .then((res) => {
          this.$refs.from.updateFieldOptions('formTable', { value: res.records })
        })
    },
    handleCancel() {
      this.$emit('input', false)
    },
    submitFun() {
      this.$refs.from.submitFun()
    },
    onSubmitFun(model) {
      wipApi.outbound.affirm({ itemList: model.formTable }).then((res) => {
        this.$message.success(this.$t('tips.optionsTxt'))
        this.$emit('ok')
        this.$emit('input', false)
      })
      // wipApi.inbound.batchSave2({ recordList: model.formTable, isVerifyWarehouse: true }).then((res) => {
      //   const list = this.entryOrderId.map((item) => {
      //     return { id: item }
      //   })
      //   wipApi.inbound.passByWarehouse({ list }).then((res) => {
      //     this.$message.success(this.$t('tips.optionsTxt'))
      //     this.$emit('ok')
      //     this.$emit('input', false)
      //   })
      // })
    }
  }
}
</script>
